package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Question;
import com.itheima.service.mongo.QuestionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Question> findListByQuestionnaireId(Object id) {
     return mongoTemplate.find(new Query(Criteria.where("questionnaireId").is(id)).with(Sort.by(Sort.Order.desc("_id"))),Question.class)  ;
    }

    @Override
    public Question findById(String questionId) {

       return mongoTemplate.findOne(new Query(Criteria.where("_id").is(new ObjectId(questionId))),Question.class);
    }
}
